import  BackPressComponent from '$/components/BackPressComponent'

export default class BackPress{

    /*创建返回事件监听器*/
    static createBackPress(fn){
        //如果返回事件已经注册就销毁
        if(BackPress.backPress){
            BackPress.willMountBackPress();
        }
        BackPress.backPress = new BackPressComponent({backPress: fn});
    }
    static isFunc(func) {
        return func && typeof func === 'function'
    }

    /*执行返回事件*/
    static didMountBackPress() {
        if(BackPress.backPress && BackPress.isFunc(BackPress.backPress.componentDidMount) ){
            BackPress.backPress.componentDidMount();
        }
    }

    /*卸载返回事件*/
   static willMountBackPress() {
        if(BackPress.backPress && BackPress.isFunc(BackPress.backPress.componentWillUnmount) ){
            BackPress.backPress.componentWillUnmount();
        }
        BackPress.backPress = null;
    }
}
